//
//  LWXRadioController.m
//  EPA
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LWXRadioController.h"
#import "LWXRedioCell.h"
#import "LWXRedioModel.h"
#import "LWXNetWorkRequestManager.h"
#import "LWXCycleView.h"
#import "LWXRadioController.h"
#import "LWXRadioListController.h"
@interface LWXRadioController ()

// 创建存数据数组
@property (nonatomic, strong) NSMutableArray *redioModelArray;
// 图片数组
@property (nonatomic, strong) NSMutableArray *imgArray;
// 创建轮播图
@property (nonatomic, strong) LWXCycleView *cycleView;
//@property (nonatomic, strong) LORefresh *headerRefresh;

@end

@implementation LWXRadioController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化数组
    self.redioModelArray = [NSMutableArray new];
    self.imgArray = [NSMutableArray new];
    // 网络请求
    [self loadData];
    // 注册 cell
    [self.tableView registerNib:[UINib nibWithNibName:@"LWXRedioCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    
    // 初始化轮播图
  self.cycleView = [[LWXCycleView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    
}
// 网络请求数据
- (void)loadData {


    [LWXNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOLIST_URL] Method:LWX_POST Params:@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {

        if (data) {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"alllist"]) {
                // 解析
                LWXRedioModel * model = [LWXRedioModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.redioModelArray addObject:model];
  
            }
            
            for (NSDictionary *itemDict in dataDict[@"carousel"]) {
            
                [self.imgArray addObject:itemDict[@"img"]];
                
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tableView reloadData];
           
                 self.cycleView.imgNameArray = self.imgArray;
                
            });
    
        }
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 200;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  
    UIView *view = self.cycleView;
    
    return view;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.redioModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LWXRedioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.redioModelArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LWXRadioListController *playVC = [[LWXRadioListController alloc] init];
    LWXRedioModel *model = self.redioModelArray[indexPath.row];
    
    playVC.radioid = model.radioid;

    [self.navigationController pushViewController:playVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
